﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace FYPHP
{
    public partial class AdminForRegister : System.Web.UI.Page
    {


        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        int u_id;


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmdUser;
                SqlDataReader dtrUser;
                conn.Open();

                cmdUser = new SqlCommand("Select * From Users where u_status = 'Pending' and u_position='Member'", conn);
                dtrUser = cmdUser.ExecuteReader();
              

                UserList.DataSource = dtrUser;
                UserList.DataBind();


                dtrUser.Close();
                conn.Close();
            }
            catch (SqlException ex)
            {

            }
        }

  

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmdUser;
                conn.Open();

                cmdUser = new SqlCommand("Update Users Set u_status = @Status where u_id=@Id ", conn);
                cmdUser.Parameters.AddWithValue("@Status", "Activate");
                cmdUser.Parameters.AddWithValue("@Id", lblUID.Text);
                cmdUser.ExecuteNonQuery();

             
                conn.Close();


                MultiView1.ActiveViewIndex = -1;
                Response.Redirect("AdminForMemberRegister.aspx");
            }
            catch (SqlException ex)
            {

            }
        }

        protected void rptAds_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
        {

            if (e.CommandName == "ViewPMember")
            {
                MultiView1.ActiveViewIndex = 0;
                u_id = Convert.ToInt32(e.CommandArgument.ToString());
              
               try
               {
                   SqlCommand cmdUser;
                   SqlDataReader dtrUser;
                   conn.Open();

                   cmdUser = new SqlCommand("Select * From Users where u_id = @Id", conn);
                   cmdUser.Parameters.AddWithValue("@Id", u_id);
                   dtrUser = cmdUser.ExecuteReader();
                   dtrUser.Read();

                   String userName = dtrUser["username"].ToString();
                   String gender = dtrUser["u_gender"].ToString();
                   String age = dtrUser["u_age"].ToString();
                   String address = dtrUser["u_address"].ToString();
                   String email = dtrUser["u_email"].ToString();
                   String rewardpoint = dtrUser["u_points"].ToString();

                   lblUID.Text = u_id.ToString();
                   lblName.Text = userName;
                   lblGender.Text = gender;
                   lblAge.Text = age;
                   lblAddress.Text = address;
                   lblEmail.Text = email;
                   lblRewardPoints.Text = rewardpoint;

                   dtrUser.Close();
                   conn.Close();
               }
               catch (SqlException ex)
               {

               }

              
            }

        }


        public String return_userpicture()
        {
          
                SqlCommand cmdUser;
                SqlDataReader dtrUser;
                conn.Open();

                cmdUser = new SqlCommand("Select * From Users where u_id =@Id", conn);

                cmdUser.Parameters.AddWithValue("@Id",u_id);
                dtrUser = cmdUser.ExecuteReader();
                  
                dtrUser.Read();
                String userPicture = dtrUser["u_picture"].ToString();

                dtrUser.Close();
                conn.Close();

            
            return userPicture;

        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmdUser;
                conn.Open();

                cmdUser = new SqlCommand("Update Users Set u_status = @Status where u_id=@Id ", conn);
                cmdUser.Parameters.AddWithValue("@Status", "Reject");
                cmdUser.Parameters.AddWithValue("@Id", lblUID.Text);
                cmdUser.ExecuteNonQuery();


                conn.Close();


                MultiView1.ActiveViewIndex = -1;
                Redirect("AdminForMemberRegister.aspx");
            }
            catch (SqlException ex)
            {

            }
        }
    }
}